package com.nekonomics
package persistence.query

import domain.detail.OrderSummary

import doobie.*
import doobie.implicits.*
import doobie.postgres.implicits.*
import zio.{ULayer, ZLayer}

trait OrderSummaryRepo {
  def weeklySummary(merchantId: Long): ConnectionIO[List[OrderSummary]]
}


object OrderSummaryRepo {
  val live: ULayer[OrderSummaryRepo] = ZLayer.succeed(OrderSummaryRepoLive)

}


object OrderSummaryRepoLive extends OrderSummaryRepo {
  override def weeklySummary(merchantId: Long): ConnectionIO[List[OrderSummary]] = {
    sql"""
         |SELECT * FROM ads.merchant_order_summary_daily_ads
         |WHERE merchant_id = $merchantId
         |ORDER BY date_key DESC
         |LIMIT 7
         |""".stripMargin
      .query[OrderSummary]
      .to[List]
  }
}